//
//  Tools.swift
//  MyProject
//
//  Created by volantPig on 2020/11/11.
//

import UIKit

class Tools: NSObject {
    
    /// 获取当前时间 ps:格式为 2019 09 09 13 33 49
    class func getCurrentTimeyyyyMMddHHmmss() -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyyMMddHHmmss"
        let currentIme = formatter.string(from: Date())
        return currentIme
    }
    
    /// 尝试将data类型数据 转换成为 String类型的json字符串
    class func dataChangeToJson(info: Data) -> String? {
        if let jsonData = try? JSONSerialization.jsonObject(with: info, options: []) {
            if let jsonString = Tools.anyChangeToJson(info: jsonData) {
                return jsonString
            }
        }
        return nil
    }
    
    /// 尝试将Any类型数据 转换成为 String类型的json字符串
    class func anyChangeToJson(info: Any) -> String? {

        guard JSONSerialization.isValidJSONObject(info) else {
            /// 转换失败
            return nil
        }
        if let jsonData = try? JSONSerialization.data(withJSONObject: info, options: []) {
            let jsonString = String.init(data: jsonData, encoding: .utf8)
            return jsonString
        }
        return nil
    }
}
